import * as types from '../mutation-types';
import localStore from 'store';
import Constants from '../../utils/constants';

// initial state
const state = {
	id: -1,
	authenticated: false,
	newMessageCount: -1,
	realName: '',
	userName: ''
};

// getters
// const getters = {// for some computed properties
	// userId: state => state.id,
	// userName: state => state.username,
	// realName: state => state.real_name
// };

// actions
const actions = {
	fetchLoginUser ({ commit }) {
		let user = localStore.get(Constants.LOGIN_USER);
		commit(types.SET_LOGIN_USER, { user });
	},
	removeLoginUser ({ commit }) {
		localStore.remove(Constants.LOGIN_USER);
		commit(types.RESET_LOGIN_USER, {});
	}
};

// mutations
const mutations = {
	[types.SET_LOGIN_USER] (state, { user }) {
		state.id = user === undefined ? '' : user.user_id;
		state.authenticated = true;
		state.newMessageCount = 0;
		state.realName = user === undefined ? '' : user.real_name;
		state.userName = user === undefined ? '' : user.username;
	},
	[types.RESET_LOGIN_USER] (state) {
		state.id = -1;
		state.authenticated = false;
		state.newMessageCount = -1;
		state.realName = '';
		state.userName = '';
	}
};

export default {
	state,
	// getters,
	actions,
	mutations
};
